package com.lgl.runner;

import com.lgl.entity.Article;
import com.lgl.service.ArticleService;
import com.lgl.utils.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class ViewCountRunner implements CommandLineRunner {
    @Autowired
    private ArticleService articleService;
    @Autowired
    private RedisCache redisCache;
    @Override
    public void run(String... args) throws Exception {
        List<Article> articleList = articleService.list();
        Map<String, Integer> HashMap = new HashMap<>();
        for (Article article : articleList) {
           HashMap.put(article.getId().toString(),article.getViewCount().intValue());
        }
        redisCache.setCacheMap("article:viewCount",HashMap);
    }
}
